package com.lei.tong.retrofit;

import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetWorkManger {
    private static NetWorkManger instance = new NetWorkManger();
    private static Retrofit retrofit;
    public static NetWorkManger getInstance(String url){
        retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(createrOkHttp())
                .build();
        return instance;
    }

    private static OkHttpClient createrOkHttp() {
        OkHttpClient client = new OkHttpClient.Builder()
                .writeTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .addNetworkInterceptor(createrLoggin())
                .build();
        return client;
    }

    private static Interceptor createrLoggin() {
        return new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC);
    }
    public <T> T creat(Class<T> service){
        return retrofit.create(service);
    }
}
